﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Entities;
using BLL;


namespace Stationery.UC
{
    public partial class UC_CookList : System.Web.UI.UserControl
    {
        BLL_Cook bll_Cook = new BLL_Cook();
        Entities.User obj_User = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj_User = (Entities.User)Session[Utils.SESSION_LOGIN];
            if (obj_User == null)
            {
                Response.Redirect(UtilsURL.home);
            }
            if (!IsPostBack)
                BindDataASPxGridView();
            else
            {
                if (Session["dataCook"] == null)
                {
                    return;
                }
                else
                {
                    DataTable table = (DataTable)Session["dataCook"];
                    DataView dv = table.DefaultView;
                    ASPxGridViewCookList.DataSource = dv;
                    ASPxGridViewCookList.DataBind();
                }
            }
        }

        #region Bind Data into Gridview
        protected void BindDataASPxGridView()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = bll_Cook.Get_Cook_All();
                if (Session["dataCook"] == null)
                {
                    if (dt == null)
                        return;
                    else
                        Session["dataCook"] = dt;
                }
                else
                {
                    dt = (DataTable)Session["dataCook"];
                }
                DataView dv = dt.DefaultView;
                ASPxGridViewCookList.DataSource = dv;
                ASPxGridViewCookList.DataBind();

            }
            catch (Exception ex)
            {
                Session["dataCook"] = null;
                dt = null;
            }
        }
        #endregion
        #region
        protected void ASPxButtonAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.cook_add, true);
        }

        protected void ImageButtonDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imageButton = sender as ImageButton;
            Int32 intID = Convert.ToInt32(imageButton.CommandArgument);

            bll_Cook.DeleteCook(intID);
            Session["data"] = null;
            Response.Redirect(Request.RawUrl);
        }
        #endregion
    }
}